package com.ken.android.music.view.activity;

import android.Manifest;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;


import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ken.android.music.mvp.B.BasePresenter;
import com.ken.android.music.view.fragment.HomeFragment;
import com.ken.android.music.view.fragment.ListFragment;
import com.ken.android.music.R;
import com.ken.android.music.account.User;
import com.ken.android.music.view.presenter.ILauncherContract;
import com.ken.android.music.view.presenter.LauncherPresenter;
import com.ken.android.music.view.view.MusicController;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Observable;
import java.util.Observer;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;

public class LauncherActivity extends BaseAudioActivity<ILauncherContract.View, LauncherPresenter>
        implements Toolbar.OnMenuItemClickListener,
        ListFragment.OnItemClickListener,
        Observer,
        View.OnClickListener ,
        ILauncherContract.View{

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private HomeFragment home;
    private Toolbar mToolbar;
    @ViewInject(R.id.iv_user_avatar)
    private ImageView mDrawerImg;
    @ViewInject(R.id.tv_username)
    private TextView view_username;

    private boolean PlayerBarToken;
//    private NavigationMenuItemView navigationView;
    private MusicController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (!User.isUserExit(this)) {
        if (false) {
            Intent intent = new Intent(LauncherActivity.this, LoginActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public int onSetContent() {
        return R.layout.activity_launch;
    }

    @Override
    public LauncherPresenter createPresenter() {
        return new LauncherPresenter();
    }

    private void initEvent() {

    }

    @Override
    public void initView(View view) {
        PlayerBarToken = true;
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        mToolbar = (Toolbar) findViewById(R.id.toolbar_home);

        controller = findViewById(R.id.controller);
        mToolbar.setTitle("");
        mToolbar.setSubtitle("");
        setSupportActionBar(mToolbar);
        mDrawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,mToolbar,R.string.drawer_open,R.string.drawer_close);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        home = new HomeFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.id_home_container, home).commit();
        //initDrawer();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        mDrawerToggle.syncState();// 这个必须要，没有的话进去的默认是个箭头。。正常应该是三横杠的
        super.onPostCreate(savedInstanceState);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.it_menu_scan:
                mPresenter.scanLocalMusic();
                break;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
//        getMenuInflater().inflate(R.menu.drawer,navigationView.createContextMenu());
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.home:

                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.it_drawer_menu3:
                Intent intent = new Intent();
                intent.putExtra("user_info", User.mUser);
                intent.setComponent(new ComponentName(LauncherActivity.this, User_InfoShowActivity.class));
                startActivity(intent);

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateToolbar(String string) {

    }

    @Override
    public void update(Observable observable, Object data) {
    }

    @Override
    public void onClick(View v) {
        //判断Service是否存在
    }



    private void initDrawer() {
        if (User.mUser != null) {
            view_username.setText(User.mUser.getUsername());
        } else {
            view_username.setText("当前无用户登录");
            mDrawerImg.setImageResource(R.drawable.music);
        }
    }

    @Override
    protected String[] needPermissions() {
        return new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MEDIA_CONTENT_CONTROL,
        };
    }
}
